package com.oneplatform.system.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jeesuite.common.annotation.ApiMetadata;
import com.jeesuite.common.constants.PermissionLevel;
import com.jeesuite.common.model.IdParam;
import com.jeesuite.common.model.Page;
import com.jeesuite.common.model.PageParams;
import com.jeesuite.common.model.SelectOption;
import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.springweb.model.PageQueryRequest;
import com.oneplatform.system.constants.SeqTimeExpr;
import com.oneplatform.system.dao.entity.SequenceRuleEntity;
import com.oneplatform.system.dto.SequenceRule;
import com.oneplatform.system.dto.param.SequenceRuleParam;
import com.oneplatform.system.dto.param.SequenceRuleQueryParam;
import com.oneplatform.system.service.SequenceRuleService;

/**
 * 
 * <br>
 * Class Name   : RegulationController
 *
 */
@RestController
@RequestMapping("/rule")
public class SequenceRuleController {

	private @Autowired SequenceRuleService sequenceRuleService;
	
	private static List<SelectOption> timeExprOptions = new ArrayList<SelectOption>();
	static{
		for (SeqTimeExpr seqTimeExpr : SeqTimeExpr.values()) {
			timeExprOptions.add(new SelectOption(seqTimeExpr.getExpr(), seqTimeExpr.name()));
		}
	}
	
	@ApiMetadata(permissionLevel = PermissionLevel.Anonymous)
	@GetMapping("timeExprs")
	public @ResponseBody List<SelectOption> timeExprOptions(){
		return timeExprOptions;
	}

	@ApiMetadata(permissionLevel = PermissionLevel.LoginRequired)
	@PostMapping("list")
	public @ResponseBody Page<SequenceRule> pageQuery(@RequestBody PageQueryRequest<SequenceRuleQueryParam> param) {
		if(param.getExample() == null){
			param.setExample(new SequenceRuleQueryParam());
		}
		Page<SequenceRule> page = sequenceRuleService.pageQuery(new PageParams(param.getPageNo(), param.getPageSize()), param.getExample());
		return page;
	}

	@ApiMetadata(permissionLevel = PermissionLevel.LoginRequired)
	@GetMapping("{id}")
	public @ResponseBody
	SequenceRule findById(@PathVariable("id") Integer id) {
		SequenceRuleEntity entity = sequenceRuleService.findRule(id);
		return BeanUtils.copy(entity, SequenceRule.class);
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired)
	@PostMapping("add")
	public @ResponseBody
	IdParam<Integer> addRegulation(@RequestBody @Validated SequenceRuleParam param) {
		param.setId(null);
		Integer id = sequenceRuleService.addRule(param);
		return new IdParam<>(id);
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired)
	@PostMapping("update")
	public @ResponseBody
	void updateRegulation(@RequestBody @Validated SequenceRuleParam param) {
		sequenceRuleService.updateRule(param);
	}

	@ApiMetadata(permissionLevel = PermissionLevel.PermissionRequired)
	@PostMapping("switch/{id}")
	public @ResponseBody
	void deleteRegulation(@PathVariable(value = "id") Integer id) {
		sequenceRuleService.enableSwitch(id);
	}

}
